package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/inventory_srv/proto"
	"sync"
)

var invClient proto.InventoryClient
var conn *grpc.ClientConn

func Init() {
	var err error
	conn, err = grpc.Dial("127.0.0.1:62547", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	invClient = proto.NewInventoryClient(conn)
}

func TestSetInv(goodsId, Num int32) {
	_, err := invClient.SetInv(context.Background(), &proto.GoodsInvInfo{
		GoodsId: goodsId,
		Num:     Num,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("设置库存成功")
}

func TestInvDetail(goodsId int32) {
	rsp, err := invClient.InvDetail(context.Background(), &proto.GoodsInvInfo{
		GoodsId: goodsId,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Num)
}

func TestSell(wg *sync.WaitGroup) {
	/*
		1. 第一件扣减成功    第二件: (1)没有库存信息 (2)库存不足
		2. 两件都扣减成功
	*/
	defer wg.Done()
	_, err := invClient.Sell(context.Background(), &proto.SellInfo{
		GoodsInfo: []*proto.GoodsInvInfo{
			{
				GoodsId: 421,
				Num:     1,
			},
			//{
			//	GoodsId: 422,
			//	Num:     30,
			//},
		},
	})
	if err != nil {
		panic(err)
	}
	//fmt.Println("库存扣减成功")
}

func TestReback() {
	_, err := invClient.Reback(context.Background(), &proto.SellInfo{
		GoodsInfo: []*proto.GoodsInvInfo{
			{
				GoodsId: 421,
				Num:     10,
			},
			{
				GoodsId: 422,
				Num:     30,
			},
		},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("库存归还成功")
}

func main() {
	Init()
	for i := 421; i <= 840; i++ {
		TestSetInv(int32(i), 100)
	}
	//TestSetInv(422, 40)
	//TestInvDetail(421)
	//TestSell()
	//TestReback()

	//var wg sync.WaitGroup
	//wg.Add(80)
	//for i := 0; i < 80; i++ {
	//	go TestSell(&wg)
	//}
	//wg.Wait()
	//conn.Close()
}
